// pages/works/works.js
let that,
  api = new (require('../../utils/api.js')),
  util = new (require('../../utils/util.js')),
  headimg = api.getimgurl(),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagesize: 20,
    currentpage: 1,
    list: [],
    nocart:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    let cartids = wx.getStorageSync("cartids");
    this.setData({ cartids });
    wx.showLoading({
      title: '加载中',
    })
    if(options.t){
      this.setData({type:options.t},()=>{
        that.getlist();
      });
    }
    
  },
  addmorecart(){
    let { nocart, list } = this.data,
      ids = nocart.join(",");
    if (nocart.length == 0) {
      wx.showToast({
        icon: 'none',
        title: '请不要重复添加',
      })
      return;
    }
   

    api.addmorecart({ ids }, res => {
      if (res.data.issuccess == 1) {
        this.setData({ nocart: [], list });
        wx.showToast({
          title: '加入成功',
        })
      }
    });
  },
  todetail(e){
    let {id} = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../books/detail/detail?id='+id,
    })
  },
  toseries(e){
  
    let { id } = e.currentTarget.dataset,{type}=this.data;
    console.log(id,type,e,"111");
    wx.navigateTo({
      url: '../series/series?t='+type+'&&i='+id,
    })
  },
  getlist(){
    let {pagesize,type,start,list} = this.data;
    if(type==1){
      // wx.setNavigationBarTitle({
      //   title: '大奖作品列表',
      // })
      api.getawards({pagesize,start},res=>{
        if(res.data.issuccess==1){
          wx.hideLoading();
          // let list = res.data.list;
          res.data.list.forEach((x, i) => {
            if (x.thumbpath.indexOf('http') == -1) {
              x.thumbpath = headimg + x.thumbpath;
              
            }
            list.push(x)
            // that.formatdata(list)
            // this.findbooksbyids(x.goodsid, list, i);
          });
          // if (list.length == 0) {
          //   this.setData({ length: 0 });
          // }
          that.setData({total:res.data.total,list,length:list.length})
        }
      });
    }else{
      // wx.setNavigationBarTitle({
      //   title: '名家大师列表',
      // })
      api.getmasters({pagesize,start},res=>{
        if(res.data.issuccess==1){
          wx.hideLoading();
          // let list = res.data.list;
          res.data.list.forEach((x, i) => {
            if (x.thumbpath.indexOf('http') == -1) {
              x.thumbpath = headimg + x.thumbpath;
            }
            list.push(x)
            // that.formatdata(list)
            // this.findbooksbyids(x.goodsid, list, i);
            
          });
          // if(list.length==0){
          //   this.setData({length:0});
          // }
          that.setData({total:res.data.total,list,length:list.length})
        }
      })
    }
  },
  
  findbooksbyids(ids, list, i) {
    api.getbooks({ ids }, res => {
      if (res.data.issuccess == 1) {
        this.formatdata(res.data.data, list, i);
      }
    });
  },

  // formatdata(clist) {
  //     let length = clist.length, { list } = this.data;
  //     list = list.concat(clist);
  //     this.setData({ list, length });
  //     wx.hideLoading();
  // },

  // formatdata(books, clist, i) {
  //   let {nocart,cartids} = this.data;
  //   books.forEach((x, i) => {
  //     if (x.thumbPicPath.indexOf("http" == -1)) {
  //       x.thumbPicPath = headimg + x.thumbPicPath;
  //     }
  //     if (cartids.indexOf(x.id) == -1) {
  //       nocart.push(x.id);
  //     } 
  //     x.detaillist = x.detaillist.split(',');
  //     x.labels = x.labels.split(',');
  //     if (x.detaillist.length > 0) {
  //       x.detaillist.forEach((y, i) => {
  //         if (y.indexOf("http") == -1) {
  //           x.detaillist[i] = headimg + x.detaillist[i];
  //         }
  //       });
  //     }
  //   });
  //   clist[i].books = books;
  //   if (clist.length - 1 == i) {
  //     let length = clist.length, { list } = this.data;
  //     list = list.concat(clist);
  //     this.setData({ list, length });
  //     wx.hideLoading();
  //   }
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {  length,total } = this.data;

    if (length >=total) {
      wx.showToast({
        icon: "none",
        title: '再怎么扯也没有了',
      })
      return;
    }
    that.setData({ start: that.data.start + that.data.pagesize }, () => {
      that.getlist()
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.logininfo.appletname,
      path: `pages/listen/listen?share=`+app.logininfo.userid,
    }
  }
})